package com.sys.text;

import com.sys.mapper.SysUserMapper;
import com.sys.entity.SysUser;
import com.sys.utils.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.ParseException;

public class SysUserText {
    @Test
    public void test() {

        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        SysUserMapper mapper = sqlSession.getMapper(SysUserMapper.class);
        int count = mapper.count();
        System.out.println(count);
        sqlSession.close();
    }

    /**
     * 实例8
     */
    @Test
    public void test2() {
        SqlSession sqlSession = null;
        List<SysUser> userlist = null;
        try {
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession();
            userlist = sqlSession.getMapper(SysUserMapper.class).selectAll();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
        if (userlist != null) {
            for (SysUser sysUser : userlist) {
                System.out.println(sysUser);
            }
        }
    }

    /**
     * 第二章示例1
     */
    @Test
    public void Test3() {
        //创建 SqlSession
        SqlSession sqlSession = MyBatisUtil.createSqlSession();

        // 创建SysUserMapper接口 的 实例
        SysUserMapper mapper = sqlSession.getMapper(SysUserMapper.class);
        // 调用其方法
        List<SysUser> list = mapper.getUsersByRealName("李");
        //循环输出
        list.forEach(sysUser -> {
            System.out.println(sysUser.getId() + "-" + sysUser.getRealName());
        });

        // 关闭 sqlSession
        MyBatisUtil.closeSqlSession(sqlSession);
    }

    /**
     * 第二章示例4
     */
    @Test
    public void test4() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        SysUser sysUser = new SysUser();
        sysUser.setRealName("李");
        sysUser.setRoleId(3);
        List<SysUser> userLiserByPojo = sqlSession.getMapper(SysUserMapper.class).getUserListByPojo(sysUser);
        userLiserByPojo.forEach(sysUser1 ->
                System.out.println(sysUser1));
        MyBatisUtil.closeSqlSession(sqlSession);
    }

    @Test
    public void test5() {
        SqlSession sqlsession = MyBatisUtil.createSqlSession();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rName", "赵");
        map.put("rId", 2);
        List<SysUser> userListByMap = sqlsession.getMapper(SysUserMapper.class).getUserListByMap(map);
        userListByMap.forEach(sysUser -> {
            System.out.println(sysUser);
        });
        MyBatisUtil.closeSqlSession(sqlsession);
    }

    /**
     * 第二章示例10
     */
    @Test
    public void test6() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<SysUser> list = sqlSession.getMapper(SysUserMapper.class).getUserListByParams("赵", 2);
        list.forEach(sysUser -> {
            System.out.println(sysUser.getRealName());
        });
        MyBatisUtil.closeSqlSession(sqlSession);
    }

    /**
     * 第二章示例17q
     */
    @Test
    public void test7() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<SysUser> userListByRoleId = sqlSession.getMapper(SysUserMapper.class).getUserListByRoleId(2);
        userListByRoleId.forEach(sysUser -> {
            System.out.println(sysUser.getSysRole().getId());
        });
    }

    /**
     * 示例21
     */
    @Test
    public void test8() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<SysUser> userAndAddressesByUserId = sqlSession.getMapper(SysUserMapper.class).getUserAndAddressesByUserId(2);
        userAndAddressesByUserId.forEach(sysUser -> {
            System.out.println(sysUser.getAddressList().size());
        });
    }

    /**
     * 示例24
     */
    @Test
    public void test9() {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            SysUser user = new SysUser();
            user.setAccount("test001");
            user.setRealName("测试用户001");
            user.setPassword("1234567");
            user.setAddress("测试地址abc");
            user.setPhone("13888888888");
            user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2019-11-11"));
            user.setSex(1);
            user.setRoleId(1);
            user.setCreatedUserId(1);
            user.setCreatedTime(new Date(System.currentTimeMillis()));
            count = sqlSession.getMapper(SysUserMapper.class).insert(user);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        System.out.println(count);
    }

    /**
     * 示例27
     */
    @Test
    public void test10() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        String pwd = "88888888";
        Integer id = 17;
        int count = 0;
        try {
            count = sqlSession.getMapper(SysUserMapper.class).updatePwd(id, pwd);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
            System.out.println(count);
        }
    }

    /**
     * 第二章示例1
     */
    @Test
    public void test11() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        String realName = "赵";
        Integer roleId = 2;
        int count = 0;
        List<SysUser> userList = null;
        try {
            userList = sqlSession.getMapper(SysUserMapper.class).selectList(realName, roleId);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
            System.out.println(count);
        }
    }

    @Test
    public void test12() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<SysUser> userList = null;
        try {
            String realName = "";
            Integer roleId = 2;
            String account = "zhong";
            Date createdTime = new SimpleDateFormat("yyyy-MM-dd").parse("2019-11-11");
            userList = sqlSession.getMapper(SysUserMapper.class).selectListByChoose(realName, roleId, account, createdTime);
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
            System.out.println(userList);
        }
    }

    @Test
    public void test13() {
        SqlSession sqlSession = null;
        List<SysUser> userList = new ArrayList<SysUser>();
        Integer[] roleIds = {1, 2};
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            userList = sqlSession.getMapper(SysUserMapper.class).getUserByRoleIdArray(roleIds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        userList.forEach(list -> {
            System.out.println(list);
        });

    }

    @Test
    public void test14() {
        SqlSession sqlSession = null;
        List<SysUser> userList = new ArrayList<SysUser>();
        List<Integer> roleIds = new ArrayList<Integer>();
        roleIds.add(1);
        roleIds.add(2);
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            userList = sqlSession.getMapper(SysUserMapper.class).getUserByRoleIdList(roleIds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        userList.forEach(list -> {
            System.out.println(list);
        });

    }

    /**
     * 示例8
     */
    @Test
    public void test15() {
        SqlSession sqlSession = null;
        List<SysUser> userList = new ArrayList<SysUser>();
        try {
            List<Integer> roleIdList = new ArrayList<Integer>();
            roleIdList.add(1);
            roleIdList.add(2);
            Map<String, Object> roleMap = new HashMap<String, Object>();
            roleMap.put("roleIdList", roleIdList);
            sqlSession = MyBatisUtil.createSqlSession();
            userList = sqlSession.getMapper(SysUserMapper.class).getUserByRoleIdMap(roleMap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        userList.forEach(list -> {
            System.out.println(list);
        });

    }
    @Test
    public void test16(){
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        int count = 0;
        try {
            SysUser user = new SysUser();
            user.setId(16);
            user.setRealName("测试用户修改");
            user.setUpdatedUserId(1);
            user.setUpdatedTime(new Date());
            count = sqlSession.getMapper(SysUserMapper.class).update(user);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
            System.out.println(count);
        }
    }
    @Test
    public void test17(){
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<SysUser> userList = new ArrayList<SysUser>();
        try{
            String realName = "";
            Integer roleId = 2;
            Integer pageIndex = 2;
            Integer pagesize = 2;
            Integer pageBegin = (pageIndex-1)*pagesize;
            userList=sqlSession.getMapper(SysUserMapper.class).selectPageList(realName,roleId,pageBegin,pagesize);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        userList.forEach(user->{
            System.out.println(user);
        });
    }

    @Test
    public void test19(){
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        List<SysUser> userList = new ArrayList<SysUser>();
        try{
            int pageSize = 5;
            int pageNo = 1;
            int startIndex = (pageNo-1)*pageSize;
            sqlSession.getMapper(SysUserMapper.class).selectListPage(startIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        userList.forEach(user->{
            System.out.println(user);
        });
    }
}
